package com.jtjrenren.android.taxi.driver.core.main;

import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDriverSr;
import com.jtjrenren.android.taxi.driver.bean.control.CmdOtherDutyOnResponse;
import com.jtjrenren.android.taxi.driver.bean.event.CommonEvent;
import com.jtjrenren.android.taxi.driver.bean.event.ControlCenterEvent;
import com.jtjrenren.android.taxi.driver.bean.event.DriverIncomeEvent;
import com.jtjrenren.android.taxi.driver.bean.event.ExitEvent;
import com.jtjrenren.android.taxi.driver.bean.event.UserInfoRefreshEvent;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.library.mvp.presenter.MvpBasePresenter;
import com.library.utils.LogUtil;
import com.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Administrator on 2016/8/26.
 */

public class MainPresenterImpl extends MvpBasePresenter<MainView, MainInterctor> implements MainPresenter {
    public MainPresenterImpl(MainView view) {
        super(view);
    }

    @Subscribe
    public void handExitEvent(ExitEvent event){
        LogUtil.log("applog", "主页，收到退出指示");
        view.finishAty();
    }

    @Subscribe
    public void handCmdEvent(ControlCenterEvent event){
        String action=event.getAction();
         if (Constants.ACTION_CONTROL_RESPONSE_OTHERDUTYON.equals(action)){
            otherDutyOn((CmdOtherDutyOnResponse)event.getResponse());
        }
    }

    //其他设备登陆的消息
    private void otherDutyOn(CmdOtherDutyOnResponse response) {
        LogUtil.log("home","本账号在其他设备登陆");
        AppManger.getInstance().showTip(response.getDesc());
        if (response.getType().equals(CmdOtherDutyOnResponse.TYPE_EXIT)){
            AppManger.getInstance().driverExit();
            view.showLoginView();
        }
    }

    @Subscribe
    public void handDriverIncomeEvent(DriverIncomeEvent event){
        ApiDriverSr apiDriverSr = event.getInfo();
        if (apiDriverSr != null) {
            String income = apiDriverSr.getMsg().getIncome();
            if (!StringUtils.isEmpty(income))
                view.refreshSr(income);
        }
    }

    @Subscribe
    public void handCommonEvent(CommonEvent event){
        int mode = event.getMode();
        if (mode == CommonEvent.MODE_REFRESH_DRIVER) {
            view.refreshDriverInfoView(AppManger.getInstance().driver);
        }
    }

    @Subscribe
    public void handRefreshUserInfo(UserInfoRefreshEvent event){
        view.refreshDriverInfoView(AppManger.getInstance().driver);
    }

    @Override
    public MainInterctor createInteractor() {
        return new MainInterctorImpl();
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        if (AppManger.getInstance().driver!=null)
            view.refreshDriverInfoView(AppManger.getInstance().driver);
    }

    @Override
    public void destory() {
       EventBus.getDefault().unregister(this);
    }

    @Override
    public void getSjSr() {
        interactor.getSjSr(AppManger.getInstance().driver.getWorkNo());
    }


}
